package com.example.testapplication;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class SecondActivity extends AppCompatActivity {

    private static final String TAG = "SecondActivity";
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        Log.d(TAG,"onCreate");

        final Button button = (Button) findViewById(R.id.buttonFinish); // 强制类型转换
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                Intent intent = new Intent();
//                //setResult(RESULT_OK,intent);
//                intent.putExtra("imooc","imooc+莫克我");
//                setResult(RESULT_OK);
                finish();
            }
        });
         // 获取传值
        if(getIntent() != null){
           //String buttonTitle = getIntent().getStringExtra(LifeCycleActivity.BUTTON_TITLE);  // 接收也要用string
            //button.setText(buttonTitle);
            // todo java.lang.String cannot be cast to android.os.Bundle
            // todo Key button_title expected Bundle but value was a java.lang.String.  The default value <null> was returned.
            // 这里报错是因为接收的类型不同导致
            // 第二种
            Bundle bundle = getIntent().getBundleExtra(LifeCycleActivity.BUTTON_TITLE);
            if(bundle != null){
                String buttonTitle = bundle.getString(LifeCycleActivity.BUTTON_TITLE);
                button.setText(buttonTitle);
            }

        }
    }
}
